﻿package quiz {
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import fl.controls.Button;
    import fl.controls.Label;
    import fl.controls.RadioButton;
    import fl.controls.RadioButtonGroup;
    import flash.display.*;
    import flash.events.MouseEvent;
    import flash.text.*;
    
    public class QuizFormat extends MovieClip
    {
        private var question:String;
		private var answers:Array;
		private var CAns:String;
		private var RBcount:int;
		private var rbg:RadioButtonGroup;
		private var questionLabel:Label;
		private var nextbut:Button;
		private var checkans:Button;	
		private var rbformat:TextFormat;
		private var rm:RightMark;
		private var wm:WrongMark;
		private var resultStr:String;
		private var seeAnswerTimer:Timer = new Timer(5000,1);
		static private var score:int = 10;
		static private var Qnum:int = 0;
	
        private var verticalSpacing:uint = 0;
		private var countdownLabel : Label;
		private var correct : Boolean = false;
        private var quizEnginge : Quiz;

		public function QuizFormat(noRB:int,Que:String,ansArr:Array,CorrectAns:String,quiz:Quiz) {
			this.quizEnginge = quiz;
			Qnum++;
			question = Que;
			answers = new Array();
			for(var i=0;i<ansArr.length;i++){
			answers[i] = ansArr[i];
			}
			CAns = CorrectAns;
			RBcount = noRB;
            setupQuiz();    
        }
        private function setupQuiz():void {
			this.seeAnswerTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endQuiz);
			setupQuestionLabel();
            setupRadioButtons();
            setupButtons();          
		}

		private function endQuiz(event : TimerEvent) : void {
			this.quizEnginge.getSeriousGame().questionTimeCompleteCorrectUnCorrect(resultStr);
			this.seeAnswerTimer.reset();
		}

		private function setupQuestionLabel():void {
			var questionFormat:TextFormat = new TextFormat();
            questionFormat.font = "Verdana";
			questionFormat.bold = true;
			questionFormat.size = 15;			
			questionFormat.color = 0xFFFFFF;
            questionLabel = new Label();
            questionLabel.text = Qnum.toString()+". "+question;
            questionLabel.autoSize = TextFieldAutoSize.LEFT;	
			questionLabel.setStyle("textFormat",questionFormat);
            questionLabel.move(50,50);            
            addChild(questionLabel);
            var countdownFormat:TextFormat = new TextFormat();
            countdownFormat.font = "Verdana";
			countdownFormat.bold = true;
			countdownFormat.size = 40;			
			countdownFormat.color = 0xFF0000;
			countdownLabel = new Label();
			countdownLabel.autoSize = TextFieldAutoSize.RIGHT;	
			countdownLabel.setStyle("textFormat",countdownFormat);
            countdownLabel.move(200,300);
			countdownLabel.text = "60";  
			addChild(countdownLabel);
			
        }

        private function setupRadioButtons():void {
            rbg = new RadioButtonGroup("question1");
			for(var i=0;i<RBcount;i++){
				createRadioButton(answers[i], rbg);
			}            
       
        }
        private function setupButtons():void {
           /* nextbut = new Button();
            nextbut.move(420,350);
            nextbut.label = "Next";
            nextbut.addEventListener(MouseEvent.CLICK, NextQuestion);
			nextbut.enabled = false;
            addChild(nextbut);*/
			checkans = new Button();
			checkans.move(420,300);
			checkans.label = "CheckAnswer";
			checkans.addEventListener(MouseEvent.CLICK, CheckAnswer);
			addChild(checkans);
        }
        private function createRadioButton(rbLabel:String, rbg:RadioButtonGroup):void {
            rbformat = new TextFormat();
            rbformat.font = "Georgia";			
			rbformat.size = 14;			
			rbformat.color = 0xFFFFFF;	   
		   
		   var rb:RadioButton = new RadioButton();
            rb.group = rbg;
            rb.label = rbLabel;
			rb.width = 600;
			rb.setStyle("textFormat",rbformat);
            rb.move(100,100+verticalSpacing);			
			verticalSpacing += 50;
            addChild(rb);			
        }
        public function NextQuestion():void {
        	this.countdownLabel.text = "60";
            if (rbg.selection == null) {
            	score = score-1;
                return;
            }
			else{			              
				
				this.correct = false;
				if(resultStr == "Correct"){
				   	
				   removeChild(rm);
			   }
			   else{
				   score = score-1;
				   if(score<8){
					   trace("verloren");
				   }
				   removeChild(wm);
			   }
				(Quiz(parent)).nextques(score);	
			}		    
        }	
		
		public function CheckAnswer(event:MouseEvent):void {
			if(!this.seeAnswerTimer.running) {
				if (rbg.selection == null) {
					trace("no answer");
					return;
				} else {
					this.quizEnginge.getSeriousGame().setQuizNoQuestionSelected(false);
					this.quizEnginge.getSeriousGame().getQuizQuestionTimer().reset();
					this.seeAnswerTimer.start();
					//nextbut.enabled = true;
					for(var i = 0;i < rbg.numRadioButtons;i++) {					
						rbg.getRadioButtonAt(i).mouseEnabled = false;
						if(rbg.getRadioButtonAt(i).label == CAns) {
							var j : int = i;
						}
					}
				  
					resultStr = (rbg.selection.label == CAns) ? "Correct" : "Incorrect";
					if(resultStr == "Correct") {
						this.correct = true;					
						rm = new RightMark();
						rm.x = rbg.selection.x - 50;
						rm.y = rbg.selection.y;
						rm.width = 35;
						rm.height = 25;
						addChild(rm);
					} else {
						this.correct = false;
						wm = new WrongMark();
						wm.x = rbg.selection.x - 50;
						wm.y = rbg.selection.y ;
						wm.width = 35;
						wm.height = 25;
						trace("add wrong: " + wm);
						addChild(wm);
						rm = new RightMark();
						rm.x = rbg.getRadioButtonAt(j).x - 50;
						rm.y = rbg.getRadioButtonAt(j).y;
						rm.width = 35;
						rm.height = 25;
						addChild(rm);
					}
				} //else
			}
		} //CheckAnswer function	
		public function setTextForCountdown(text:String){
			this.countdownLabel.text = text;
		}
		
		public function isCorrect():Boolean{
			return this.correct;
		}
	}  //class
}  //package
